package com.rickpan.entity;

import jakarta.persistence.*;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import java.time.LocalDateTime;

/**
 * 用户隐私设置实体类
 */
@Entity
@Table(name = "user_privacy_settings", 
       uniqueConstraints = @UniqueConstraint(columnNames = {"user_id"}))
@EntityListeners(AuditingEntityListener.class)
public class UserPrivacySettings {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "user_id", nullable = false, unique = true)
    private Long userId;

    @Column(name = "allow_search_by_username", nullable = false)
    private Boolean allowSearchByUsername = true;

    @Column(name = "allow_search_by_email", nullable = false)
    private Boolean allowSearchByEmail = false;

    @Column(name = "allow_search_by_real_name", nullable = false)
    private Boolean allowSearchByRealName = false;

    @Column(name = "show_online_status", nullable = false)
    private Boolean showOnlineStatus = true;

    @Column(name = "allow_friend_requests", nullable = false)
    private Boolean allowFriendRequests = true;

    @Column(name = "auto_accept_team_members", nullable = false)
    private Boolean autoAcceptTeamMembers = false;

    @CreatedDate
    @Column(name = "created_at", nullable = false, updatable = false)
    private LocalDateTime createdAt;

    @LastModifiedDate
    @Column(name = "updated_at", nullable = false)
    private LocalDateTime updatedAt;

    // 关联关系
    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "user_id", insertable = false, updatable = false)
    private User user;

    // 构造函数
    public UserPrivacySettings() {}

    public UserPrivacySettings(Long userId) {
        this.userId = userId;
    }

    // 业务方法
    public boolean canBeSearchedBy(String searchType) {
        switch (searchType.toUpperCase()) {
            case "USERNAME":
                return allowSearchByUsername;
            case "EMAIL":
                return allowSearchByEmail;
            case "REAL_NAME":
                return allowSearchByRealName;
            default:
                return false;
        }
    }

    public boolean canReceiveFriendRequest() {
        return allowFriendRequests;
    }

    public boolean shouldShowOnlineStatus() {
        return showOnlineStatus;
    }

    public boolean shouldAutoAcceptTeamMembers() {
        return autoAcceptTeamMembers;
    }

    // Getter和Setter方法
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Long getUserId() {
        return userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }

    public Boolean getAllowSearchByUsername() {
        return allowSearchByUsername;
    }

    public void setAllowSearchByUsername(Boolean allowSearchByUsername) {
        this.allowSearchByUsername = allowSearchByUsername;
    }

    public Boolean getAllowSearchByEmail() {
        return allowSearchByEmail;
    }

    public void setAllowSearchByEmail(Boolean allowSearchByEmail) {
        this.allowSearchByEmail = allowSearchByEmail;
    }

    public Boolean getAllowSearchByRealName() {
        return allowSearchByRealName;
    }

    public void setAllowSearchByRealName(Boolean allowSearchByRealName) {
        this.allowSearchByRealName = allowSearchByRealName;
    }

    public Boolean getShowOnlineStatus() {
        return showOnlineStatus;
    }

    public void setShowOnlineStatus(Boolean showOnlineStatus) {
        this.showOnlineStatus = showOnlineStatus;
    }

    public Boolean getAllowFriendRequests() {
        return allowFriendRequests;
    }

    public void setAllowFriendRequests(Boolean allowFriendRequests) {
        this.allowFriendRequests = allowFriendRequests;
    }

    public Boolean getAutoAcceptTeamMembers() {
        return autoAcceptTeamMembers;
    }

    public void setAutoAcceptTeamMembers(Boolean autoAcceptTeamMembers) {
        this.autoAcceptTeamMembers = autoAcceptTeamMembers;
    }

    public LocalDateTime getCreatedAt() {
        return createdAt;
    }

    public void setCreatedAt(LocalDateTime createdAt) {
        this.createdAt = createdAt;
    }

    public LocalDateTime getUpdatedAt() {
        return updatedAt;
    }

    public void setUpdatedAt(LocalDateTime updatedAt) {
        this.updatedAt = updatedAt;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    @Override
    public String toString() {
        return "UserPrivacySettings{" +
                "id=" + id +
                ", userId=" + userId +
                ", allowSearchByUsername=" + allowSearchByUsername +
                ", allowSearchByEmail=" + allowSearchByEmail +
                ", allowSearchByRealName=" + allowSearchByRealName +
                ", showOnlineStatus=" + showOnlineStatus +
                ", allowFriendRequests=" + allowFriendRequests +
                ", autoAcceptTeamMembers=" + autoAcceptTeamMembers +
                ", createdAt=" + createdAt +
                ", updatedAt=" + updatedAt +
                '}';
    }
}
